import 'package:flutter_oldman_health/models/response_data_model.dart';
import 'package:flutter_oldman_health/models/login_model.dart';
import 'package:flutter_oldman_health/http/api_client.dart';
import 'package:flutter_oldman_health/models/register_model.dart';
import 'package:flutter_oldman_health/utils/utils.dart';

class UserDao {
  static String enterpriseId = 'f982b9387bc74aa482a9705d8587db8a';

  static Future<LoginModel> login(Login login) async {
    try {
      LoginModel model = await ApiClient().login(login);
      if (model.success()) {
        Utils.putStrSharePreferences('userId', model.data?.user?.id);
      }
      return model;
    } catch (e) {
      return LoginModel(code: -1, data: null, msg: e.toString());
    }
  }

  static Future<ResponseDataModel<String>>  register(Register register) async {
    try {
      ResponseDataModel<String> model =
          await ApiClient().register(enterpriseId, register);

      if (model.success()) {
        // Utils.putStrSharePreferences('userId', model.data?.user?.id);
        // SpUtil.putObject('userId', model.data);
      }
      print(model.toJson().toString() +
          "UserDao  register  " +
          (model.success()).toString());

      return model;
    } catch (e) {
      return ResponseDataModel(code: -1, data: null, msg: e.toString());
    }
  }

  static Future<ResponseDataModel<String>> requestVerificationCode(
      String phone) async {
    try {
      ResponseDataModel<String> model;
      model = await ApiClient()
          .requestVerificationCode(phone) ;

      print(model.toJson().toString() +
          "UserDao  success  " +
          (model.success()).toString());
      return model;
    } catch (e) {
      return ResponseDataModel(code: -1, data: null, msg: e.toString());
    }
  }
}
